package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${superServiceImplClassPackage};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import com.lovol.farm.base.service.common.ResultEnum;
import com.lovol.farm.base.service.common.exception.BusinessException;
import com.lovol.farm.base.service.common.ResultResponse;
import org.springframework.stereotype.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ${table.mapperName} mapper;

    @Override
    public ResultResponse doInsert(${entity} bean) {
        boolean status = this.save(bean);
        if (!status) {//不符合条件，数据库表数据回滚
            throw new BusinessException(ResultEnum.SYSTEM_EXCEPTION);
        }
        return ResultResponse.ok(bean);
    }

    @Override
    public ResultResponse doDeleteById(String id) {
        if (null == id || id.isEmpty()) {
            return ResultResponse.fail("id不能为空!");
        }
        boolean status = this.removeById(id);
        if (!status) {
            throw new BusinessException(ResultEnum.SYSTEM_EXCEPTION);
        }
        return ResultResponse.ok();
    }

    @Override
    public ResultResponse doUpdate(${entity} bean) {
        boolean status = this.updateById(bean);
        if (!status) {
            throw new BusinessException(ResultEnum.SYSTEM_EXCEPTION);
        }
        return ResultResponse.ok(bean);
    }

    @Override
    public ResultResponse getItemById(String id) {
        try {
            ${entity} bean = this.getById(id);
            if (null == bean) {
                return ResultResponse.fail(9999, "没有查询到结果");
            }
            return ResultResponse.ok(bean);
        } catch (BusinessException e) {
            return ResultResponse.fail(e.getMsg());
        }
    }

    @Override
    public ResultResponse pageCondition(int page, int limit,QueryWrapper queryWrapper) {
        Page<${entity}> pageData = new Page<>(page, limit);
        IPage<${entity}> iPage = this.page(pageData, queryWrapper);
        return ResultResponse.ok(iPage);
    }

    @Override
    public ResultResponse customQuery(QueryWrapper queryWrapper) {
        List<${entity}> records =  this.baseMapper.selectList(queryWrapper);
        return ResultResponse.ok(records);
    }

    @Override
    public ResultResponse queryByPolygon(String geomWkt) {
        List<${entity}> records = mapper.queryByPolygon(geomWkt);
        return ResultResponse.ok(records);
    }
}
</#if>
